import {serverUrl} from "@/settings.js";
import throttle from "../uview-ui/libs/function/throttle.js"
import getToken from "./token.js"

// const requestUrl = 'http://127.0.0.1:8081';

// const requestUrl = 'http://zirenx.cn1.utools.club';
// const appid = 'wx01e91c86f18024eb';
// const appid = 'wx3f5f1943bcce32d6';
// const appid = 'wx63639042dd740ffc';
const requestUrl = serverUrl;
const exclude = ['/app/login', '/captchaImage']; // 排除token验证
var urlMap = {};

export default function request(param) {
	uni.showLoading({
		title: '加载中'
	});
	return new Promise(resolve => {
		param = param || {};
		let url = param.url;
		let method = param.method;
		let data = param.data || param.params || {};
		

		// if (url.indexOf('appid') == -1 || data['appid'] == undefined) {
		// 	data['appid'] = appid;
		// }
		clearDeep(data);
		let header = {};
		let isExclude = false; //是否排除

		if (exclude.length > 0) {
			exclude.filter(e => {
				if (e == url) {
					isExclude = true;
				}
			})
		}
		urlMap[url] = 0;
		
		if (!isExclude) {
			let token = uni.getStorageSync('token');
			if (token == '') {
				getToken().then(d=>{
					header['Authorization'] = 'Bearer ' + d;
					getRequest(url,method,data,header,isExclude).then(res=>{
						resolve(res);
					});
				})
			} else {
				header['Authorization'] = 'Bearer ' + token;
				getRequest(url,method,data,header,isExclude).then(res=>{
					resolve(res);
				});
			}
		}else{
			getRequest(url,method,data,header,isExclude).then(res=>{
				resolve(res);
			});
		}
	})
}

function getRequest(url,method,data,header,isExclude){
	// console.log(url,method,data,header)
	
	urlMap[url] = urlMap[url]+1;
	return new Promise(resolve => {
			uni.request({
				url: requestUrl + url,
				method: method,
				data: data,
				header: header,
				fail(res) {
					console.log(res)
					uni.showToast({
						title: '获取失败，请检查网络',
						icon: 'none'
					})
				},
				complete(res) {
					uni.hideLoading()
					// 未登录,token过期
					if (res.code == 401 || res.data.code == 401) {
						console.log('token过期',urlMap[url])
						uni.removeStorageSync('token');
						
						if (!isExclude) {
							if(urlMap[url] < 3){
								getToken().then(d=>{
									console.log('token过期后，重新获取');
									
									header['Authorization'] = 'Bearer ' + d;
									getRequest(url,method,data,header).then(res=>{
										resolve(res);
									});
								})
							}
						}
					}else{
						resolve(res);
					}
				}
			})
	});
}

//清理对象某些比如null，undefined，"",函数
function clearDeep(obj) {
	if (!obj || !typeof obj === 'object') return

	const keys = Object.keys(obj)
	for (var key of keys) {
		const val = obj[key]
		if (
			typeof val === 'undefined' ||
			((typeof val === 'object' || typeof val === 'string') && !val)
		) {
			// 如属性值为null或undefined或''，则将该属性删除
			delete obj[key]
		} else if (typeof val === 'object') {
			// 属性值为对象，递归调用
			clearDeep(obj[key])

			if (Object.keys(obj[key]).length === 0) {
				// 如某属性的值为不包含任何属性的独享，则将该属性删除
				delete obj[key]
			}
		}
	}
}
